package db.seed;

import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.time.LocalDateTime;

import static com.mai4j.maiadmin.generated.Tables.USER_GROUP;
import static com.mai4j.maiadmin.generated.Tables.USER_GROUP_DESCRIPTION;
import static db.seed.V20230404162156__Insert_AdminMenu.DEFAULT_ADMIN_MENU_ID;
import static org.jooq.impl.DSL.using;


/**
 * 初始化用户组
 */
@Slf4j
public class V20230404162223__Insert_UserGroup extends BaseJavaMigration {

    static final Long ADMIN_GROUP_ID = 288888888888888888L;
    static final String ADMIN_GROUP_CODE = "admin";

    @Override
    public void migrate(Context context) {
        using(context.getConnection())
                .insertInto(USER_GROUP)
                .columns(
                        USER_GROUP.USER_GROUP_ID,
                        USER_GROUP.GROUP_CODE,
                        USER_GROUP.PERMISSION,
                        USER_GROUP.ADMIN_MENU_ID,
                        USER_GROUP.DATE_ADDED,
                        USER_GROUP.DATE_MODIFIED
                )
                .values(ADMIN_GROUP_ID,
                        ADMIN_GROUP_CODE,
                        "[*]",
                        DEFAULT_ADMIN_MENU_ID,
                        LocalDateTime.now(),
                        LocalDateTime.now())
                .execute();
        using(context.getConnection())
                .insertInto(USER_GROUP_DESCRIPTION)
                .columns(
                        USER_GROUP_DESCRIPTION.USER_GROUP_ID,
                        USER_GROUP_DESCRIPTION.LANGUAGE_CODE,
                        USER_GROUP_DESCRIPTION.NAME,
                        USER_GROUP_DESCRIPTION.DESCRIPTION
                )
                .values(ADMIN_GROUP_ID,
                        "zh-CN",
                        "超级管理员",
                        "超级管理员")
                .values(ADMIN_GROUP_ID,
                        "en-US",
                        "Super admin",
                        "Super admin")
                .execute();
    }
}